package com.ruoyi.supplier.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.supplier.domain.TbPurchase;
import com.ruoyi.supplier.domain.TbPurchaseDetail;
import com.ruoyi.supplier.service.ITbPurchaseDetailService;
import com.ruoyi.supplier.service.ITbPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.mapper.TbCheckProductDetailMapper;
import com.ruoyi.supplier.domain.TbCheckProductDetail;
import com.ruoyi.supplier.service.ITbCheckProductDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 验货明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-11
 */
@Service
public class TbCheckProductDetailServiceImpl implements ITbCheckProductDetailService 
{
    @Autowired
    private TbCheckProductDetailMapper tbCheckProductDetailMapper;
    @Autowired
    private ITbPurchaseDetailService tbPurchaseDetailServiceImpl;

    /**
     * 查询验货明细
     * 
     * @param id 验货明细ID
     * @return 验货明细
     */
    @Override
    public TbCheckProductDetail selectTbCheckProductDetailById(Long id)
    {
        return tbCheckProductDetailMapper.selectTbCheckProductDetailById(id);
    }

    /**
     * 查询验货明细列表
     * 
     * @param tbCheckProductDetail 验货明细
     * @return 验货明细
     */
    @Override
    public List<TbCheckProductDetail> selectTbCheckProductDetailList(TbCheckProductDetail tbCheckProductDetail)
    {
        return tbCheckProductDetailMapper.selectTbCheckProductDetailList(tbCheckProductDetail);
    }

    /**
     * 新增验货明细
     * 
     * @param tbCheckProductDetail 验货明细
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbCheckProductDetail(TbCheckProductDetail tbCheckProductDetail)
    {
        tbCheckProductDetail.setCreateTime(DateUtils.getNowDate());
        tbCheckProductDetail.setCreateBy(SecurityUtils.getUsername());
        Integer checkStatus = tbCheckProductDetail.getCheckStatus();
        Long checkType = tbCheckProductDetail.getCheckType();
        // 提交 && 验货入库
        if(checkStatus==1&&checkType==0){
            TbPurchaseDetail tbPurchaseDetail=new TbPurchaseDetail();
            tbPurchaseDetail.setId(tbCheckProductDetail.getPurchaseDetailId());
            tbPurchaseDetail.setCheckQty(tbCheckProductDetail.getQty());
            tbPurchaseDetailServiceImpl.updateTbPurchaseDetail(tbPurchaseDetail);
        }

        return tbCheckProductDetailMapper.insertTbCheckProductDetail(tbCheckProductDetail);
    }

    /**
     * 修改验货明细
     * 
     * @param tbCheckProductDetail 验货明细
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTbCheckProductDetail(TbCheckProductDetail tbCheckProductDetail)
    {
        tbCheckProductDetail.setUpdateTime(DateUtils.getNowDate());
        tbCheckProductDetail.setUpdateBy(SecurityUtils.getUsername());
        Integer checkStatus = tbCheckProductDetail.getCheckStatus();
        Long checkType = tbCheckProductDetail.getCheckType();
        Long purchaseDetailId = tbCheckProductDetail.getPurchaseDetailId();
        TbPurchaseDetail tbDetail = tbPurchaseDetailServiceImpl.selectTbPurchaseDetailById(purchaseDetailId);
        Integer purchaseStatus = tbDetail.getPurchaseStatus();
        // 是否已经全部验货，如果全部验货，则此单不能在进行验货
        if(purchaseStatus==1){
            return -1;
        }

        // 提交 && 验货入库
        if(checkStatus==1&&checkType==0){
            TbPurchaseDetail tbPurchaseDetail=new TbPurchaseDetail();
            tbPurchaseDetail.setId(purchaseDetailId);
            tbPurchaseDetail.setCheckQty(tbCheckProductDetail.getQty());
            tbPurchaseDetailServiceImpl.updateTbPurchaseDetail(tbPurchaseDetail);
        }
        return tbCheckProductDetailMapper.updateTbCheckProductDetail(tbCheckProductDetail);
    }

    /**
     * 批量删除验货明细
     * 
     * @param ids 需要删除的验货明细ID
     * @return 结果
     */
    @Override
    public int deleteTbCheckProductDetailByIds(Long[] ids)
    {
        for (Long id:ids ) {
            TbCheckProductDetail tbCheckProductDetail = tbCheckProductDetailMapper.selectTbCheckProductDetailById(id);
            if(tbCheckProductDetail.getCheckStatus()>0){

                return  -1;
            }
        }
        return tbCheckProductDetailMapper.deleteTbCheckProductDetailByIds(ids);
    }

    /**
     * 删除验货明细信息
     * 
     * @param id 验货明细ID
     * @return 结果
     */
    @Override
    public int deleteTbCheckProductDetailById(Long id)
    {
        return tbCheckProductDetailMapper.deleteTbCheckProductDetailById(id);
    }

    @Override
    public List<TbCheckProductDetail> selectTbCheckProductDetaiMinPrice() {
        return tbCheckProductDetailMapper.selectTbCheckProductDetaiMinPrice();
    }
}
